/*
 *所有关于tb_project_fundingtemplatesource_relation类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_fundingtemplatesource_relationService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using MYData.Core.EFDbContext;
using System.Threading.Tasks;
using MYData.Entity.DTO.ApiEntity.Input;
using Mapster;
using MYData.Entity.DTO.ApiEntity.OutPut;

namespace MYData.System.Services
{
    public partial class tb_project_fundingtemplatesource_relationService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_fundingtemplatesource_relationRepository _repository;//访问数据库
        private readonly VOLContext _context;

        [ActivatorUtilitiesConstructor]
        public tb_project_fundingtemplatesource_relationService(
            Itb_project_fundingtemplatesource_relationRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
,
            VOLContext context)
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _context = context;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 根据项目来源单位查询出项目来源单位对应的项目模板
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetProjectFundingTemplateBySourcePartAsync(string SourceId)
        {
            var list = from x in _context.Set<tb_project_fundingtemplatesource_relation>().ToList()
                       join y in _context.Set<tb_project_sourceinfo>().ToList() on x.sourcePartId equals y.Id
                       join z in _context.Set<Tb_Project_Fundtemplate>().ToList() on x.templateId equals z.fundtemplateId
                       where z.isDelete == 0
                       select new Project_FundtemplateOutputDto
                       {
                           fundtemplateFunsRatio = z.fundtemplateFunsRatio,
                           fundtemplateFunsRatioName = z.fundtemplateFunsRatioName,
                           fundtemplateId = z.fundtemplateId,
                           fundtemplateName = z.fundtemplateName,
                           fundtemplateState = z.fundtemplateState,
                           isSupportingFunds = z.isSupportingFunds,
                           sourcePartId = x.sourcePartId,
                           state = true,
                           children = Tb_Project_Templatesubject_RelationshipService.Instance.GetBudgeSubjectTemp2(x.templateId)
                       };
            if (!string.IsNullOrEmpty(SourceId))
            {
                list = list.Where(x => x.sourcePartId.Equals(SourceId));
            }
            return webResponse.OK("获取项目模板成功!", list);
        }

        public void InsertItem(tb_project_fundingtemplatesource_relation item)
        {
            _repository.Add(item);
        }

    }
}
